/**
 * @(#)com.framework.web.demo.repository.DemoRepository
 *
 * Copyright (c) 2014-2018 上海天跃科技有限公司
 *
 * DESC:
 *
 */
package com.framework.web.sys.repository;

import com.framework.web.sys.domain.Resource;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 资源信息
 *
 * @author fusf
 * @version 1.0  2016/1/7
 */
public interface ResourceRepository extends JpaRepository<Resource, String>,
        JpaSpecificationExecutor<Resource> {


    /**
     * 根据sortIndex排序，查询全部资源
     *
     * @return
     */
    @Query("select o from Resource o  order by o.sortIndex")
    public List<Resource> findAllOrderBySortIndex();

    /**
     * 根据sortIndex排序，查询有效的资源
     *
     * @return
     */
    @Query("select o from Resource o where o.status='0' order by o.sortIndex")
    public List<Resource> findAllByStatusOrderBySortIndex();

    /**
     * 根据状态和类型获取资源列表
     *
     * @param status
     * @param resourceType
     * @return
     */
    public List<Resource> findByStatusAndResourceTypeOrderBySortIndex(String status, String resourceType);

    /**
     * 根据员工Id获取父节点列表
     * @param userId
     * @return
     */
   @Query("select r from UserRole ur,Role roles,RoleResource rolesRes,Resource r  where ur.roleId = roles.id and roles.id = rolesRes.roleId and r.id = rolesRes.resourceId and ur.userId = ?1  and r.resourceType = 10  and r.status='0' and r.parentId is null order by r.sortIndex ")
    public List<Resource> findParentByUserId(String userId);

	/**
     * 根据用户获取子节点列表
     * @param userId
     * @return
     */
	@Query("select r from UserRole ur,Role roles,RoleResource rolesRes,Resource r  where ur.roleId = roles.id and roles.id = rolesRes.roleId and r.id = rolesRes.resourceId and ur.userId = ?1  and r.resourceType = 10  and r.status='0' and r.parentId is not null order by r.sortIndex ")
	public List<Resource> findChildrenByUserId(String userId);

    /**
     * 超级用户获取父节点列表
     *
     * @return
     */
    @Query("select o from Resource o where  o.status = '0' and o.resourceType = '10' and o.parentId is  null order by o.sortIndex")
    public List<Resource> findParentBySuperManage();

    /**
     * 超级用户获取子节点列表
     *
     * @return
     */
    @Query("select o from Resource o where  o.status = '0' and o.resourceType = '10' and o.parentId is not null order by o.sortIndex")
    public List<Resource> findChildrenBySuperManage();

    /**
     * 获取用户资源权限
     *
     * @param userId
     * @return
     */
    @Query("select r from UserRole ur,Role role,RoleResource rolesRes,Resource r  where ur.roleId = role.id and role.id = rolesRes.roleId and r.id = rolesRes.resourceId and ur.userId = ?1 and r.resourceType = 20    order by r.sortIndex ")
    public List<Resource> findAuthority(String userId);

    /**
     * 根据资源类型和状态
     * @param status
     * @param resourceType
     * @return
     */
    public List<Resource> findByStatusAndResourceType(String status,String resourceType);

}
